﻿/*----------------------------------------------------------------
 * 
 * 版权所有：Copyright (C) 2013
 * 
 * 文件名：IRepository.cs
 * 文件功能描述：
 * 
 * 
 * 作 者：zhangq
 * 邮 箱：ifunnyface@outlook.com
 * 日 期：2013-01-23 09:17:17
 * 
 * 
 * 修改标识： Author
 * 修改描述： 相关描述
 * 
 * ---------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Common.Extensions.OrderBy;

namespace Infrastructure
{

    /// <summary>
    /// 操作仓接口
    /// </summary>
    public interface IRepository<T> where T : class
    {

        IUnitOfWork UnitOfWork { get; set; }

        #region 增、删、改

        /// <summary>
        ///   Create a new object to database.
        /// </summary>
        /// <param name="entity"> Specified a new object to create. </param>
        /// <returns>T</returns>
        T Create(T entity);

        /// <summary>
        ///   Deletes the object by primary key
        /// </summary>
        /// <param name="id"> </param>
        void Delete(object id);

        /// <summary>
        ///   Delete the object from database.
        /// </summary>
        /// <param name="entity"> Specified a existing object to delete. </param>
        void Delete(T entity);

        /// <summary>
        ///   Delete objects from database by specified filter expression.
        /// </summary>
        /// <param name="predicate"> </param>
        void Delete(Expression<Func<T, bool>> predicate);

        /// <summary>
        ///   Update object changes and save to database.
        /// </summary>
        /// <param name="entity"> Specified the object to save. </param>
        void Update(T entity);

        #endregion


        #region find one/查询一条记录

        /// <summary>
        ///   Gets object by primary key.
        /// </summary>
        /// <param name="id"> primary key </param>
        /// <returns>T</returns>
        T Find(object id);


        /// <summary>
        ///   Gets object by primary key.
        /// </summary>
        /// <param name="keys"> primary keys </param>
        /// <returns>T</returns>
        T Find(params object[] keys);


        /// <summary>
        ///   Find object by specified expression.
        /// </summary>
        /// <param name="predicate">filter</param>
        /// <returns>T</returns>
        T Find(Expression<Func<T, bool>> predicate, IList<string> includes = null);

        #endregion


        /// <summary>
        ///   Get the total objects count.
        /// </summary>
        int Count(Expression<Func<T, bool>> predicate);


        #region find results by filter

        /// <summary>
        ///   Gets all objects from database
        /// </summary>
        /// <returns>IEnumerable-T</returns>
        IEnumerable<T> LoadAll();


        /// <summary>
        ///  Gets objects from database by filter.
        /// </summary>
        /// <param name="predicate"> filter </param>
        /// <param name="orderByExpressions"> OrderByExpressions </param>
        /// <returns>IEnumerable-T</returns>
        IEnumerable<T> Filter(Expression<Func<T, bool>> predicate,
                              params IOrderByExpression<T>[] orderByExpressions);


        /// <summary>
        /// Gets objects from database by filter.
        /// </summary>
        /// <param name="predicate">filter</param>
        /// <param name="pageIndex">the current page index</param>
        /// <param name="pageSize">records in the current page</param>
        /// <param name="orderByExpressions"> OrderByExpressions </param>
        /// <returns>IEnumerable-T</returns>
        IEnumerable<T> Filter(Expression<Func<T, bool>> predicate,
                              int pageIndex,
                              int pageSize,
                              params IOrderByExpression<T>[] orderByExpressions);



        /// <summary>
        /// Gets objects from database by filter.
        /// </summary>
        /// <param name="predicate">filter</param>
        /// <param name="pageIndex">the current page index</param>
        /// <param name="pageSize">records in the current page</param>
        /// <param name="includes">includes field</param>
        /// <param name="orderByExpressions"> OrderByExpressions </param>
        /// <returns>IEnumerable-T</returns>
        IEnumerable<T> Filter(Expression<Func<T, bool>> predicate,
                              int pageIndex,
                              int pageSize,
                              IList<string> includes, params IOrderByExpression<T>[] orderByExpressions);

        #endregion



        /// <summary>
        ///   Gets the object(s) is exists in database by specified filter.
        /// </summary>
        /// <param name="predicate"> Specified the filter expression </param>
        /// <returns>bool</returns>
        bool Contains(Expression<Func<T, bool>> predicate);


    }

}